package com.demo.java8;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by hunter
 *
 * @Description stream test
 */
public class StreamTest {

    /**
     * 一些简单的java8语法糖 demo
     *
     * @param args
     */
    public static void main(String[] args) {
        List<TestPojo> pojoList = new ArrayList<>();
        TestPojo pojo1 = new TestPojo(1, "1号", 1);
        pojoList.add(pojo1);
        TestPojo pojo2 = new TestPojo(2, "2号", 2);
        pojoList.add(pojo2);
        TestPojo pojo3 = new TestPojo(3, "3号", 1);
        pojoList.add(pojo3);
        TestPojo pojo4 = new TestPojo(4, "4号", 2);
        pojoList.add(pojo4);
        TestPojo pojo5 = new TestPojo(5, "5号", 3);
        pojoList.add(pojo5);

        pojoList.stream().filter(pojo -> pojo.getId() > 2).forEach(System.out::println);

        System.out.println("~ -------------------- ");

        System.out.println(pojoList.stream().collect(Collectors.groupingBy(TestPojo::getType)));

        System.out.println("~ -------------------- ");

        System.out.println(pojoList.stream().sorted((p1, p2) -> p2.getType() - p1.getType())
                .collect(Collectors.toList()));
    }
}
